package myadmin.jtthink.com.MyServices;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import myadmin.jtthink.com.Mapper.*;
import myadmin.jtthink.com.Models.*;
import myadmin.jtthink.com.MyAnnotations.GET;
import myadmin.jtthink.com.MyAnnotations.MethodName;
import myadmin.jtthink.com.MyAnnotations.POST;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class AuthService {

    @Autowired
    RoleMapper roleMapper;
    @Autowired
    AccessMapper accessMapper;
    @Autowired
    RUserMapper rUserMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    RoleAccessMapper roleAccessMapper;

    @POST
    @MethodName("rolelist") //角色列表
    public AuthResponse rolelist(@RequestBody ListOption listOption){
        QueryWrapper<RoleModel> queryWrapper=new QueryWrapper<>();
        if(!listOption.getKey().trim().equals("")){
            queryWrapper.like("role_name",listOption.getKey().trim());
        }
        Page<RoleModel> p=new Page<>(listOption.getPageNo(),listOption.getPageSize());
        IPage<RoleModel> result= this.roleMapper.selectPage(p,queryWrapper);
        return AuthResponse.build(result.getRecords(),
                result.getCurrent(),result.getSize(),result.getTotal());

    }
    @POST
    @MethodName("rolesave") //保存角色
    public int rolesave(@RequestBody RoleModel roleModel){
        if(roleModel.getId()>0){
            return this.roleMapper.updateById(roleModel);
        }
        return this.roleMapper.insert(roleModel);
    }
    @POST //角色详细
    @MethodName("roledetail") //获取角色详细
    public RoleModel getrole(@RequestBody RoleModel roleModel){
        return this.roleMapper.selectById(roleModel.getId());
    }


    @POST
    @MethodName("access_list") //获取权限列表
    public AuthResponse accesslist(@RequestBody ListOption listOption){
        QueryWrapper<AccessModel> queryWrapper=new QueryWrapper<>();
        if(!listOption.getKey().trim().equals("")){
            queryWrapper.like("access_name",listOption.getKey().trim());
        }
        Page<AccessModel> p=new Page<>(listOption.getPageNo(),listOption.getPageSize());
        IPage<AccessModel> result= this.accessMapper.selectPage(p,queryWrapper);
        return AuthResponse.build(result.getRecords(),
                result.getCurrent(),result.getSize(),result.getTotal());

    }
    @POST
    @MethodName("access_save") //保存权限
    public int accesssave(@RequestBody AccessModel accessModel){
        if(accessModel.getId()>0){
            return this.accessMapper.updateById(accessModel);
        }
        return this.accessMapper.insert(accessModel);
    }
    @POST
    @MethodName("access_detail") //获取权限详细
    public AccessModel accessdetail(@RequestBody AccessModel accessModel){
        return this.accessMapper.selectById(accessModel.getId());
    }


    @GET
    @MethodName("role_access")
    public List<RoleAccessModel> getRoleAccess(@RequestParam(name = "rid",defaultValue = "0") String role_id){
        return roleAccessMapper.getRoleAccess(Integer.parseInt(role_id));
    }

    @POST
    @MethodName("ra_save")
    public boolean roleAccessSave(@RequestBody List<RoleAccessModel> roleAccessModels,
                                  @RequestParam(name = "rid") String role_id){
        QueryWrapper<RoleAccessModel> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_id",role_id);
        this.roleAccessMapper.delete(queryWrapper);
        for( RoleAccessModel roleAccessModel :roleAccessModels){
            this.roleAccessMapper.insert(roleAccessModel);
        }
        return true;
    }

    @POST
    @MethodName("ruserlist") // 用户列表
    public AuthResponse ruserlist(@RequestBody ListOption listOption){
        QueryWrapper<RUserModel> queryWrapper=new QueryWrapper<>();
        if(!listOption.getKey().trim().equals("")){
            queryWrapper.like("user_name",listOption.getKey().trim());
        }
        Page<RUserModel> p=new Page<>(listOption.getPageNo(),listOption.getPageSize());
        IPage<RUserModel> result= this.rUserMapper.selectPage(p,queryWrapper);
        return AuthResponse.build(result.getRecords(),
                result.getCurrent(),result.getSize(),result.getTotal());

    }
    @POST
    @MethodName("rusersave") //保存用户
    public int rusersave(@RequestBody RUserModel rUserModel){
        if(rUserModel.getId()>0){
            return this.rUserMapper.updateById(rUserModel);
        }
        return this.rUserMapper.insert(rUserModel);
    }

    @POST //用户详细
    @MethodName("ruserdetail") //获取用户详细
    public RUserModel ruserdetail(@RequestBody RUserModel rUserModel){
        return this.rUserMapper.selectById(rUserModel.getId());
    }
    @GET
    @MethodName("user_role") //获取某个用户的角色
    public List<UserRoleModel> getUserRole(@RequestParam(name = "uid",defaultValue = "0") String user_id){
        return userRoleMapper.getUserRole(Integer.parseInt(user_id));
    }

    @POST
    @MethodName("user_role_save")
    public boolean userRoleSave(@RequestBody List<UserRoleModel> userRoleModels,
                                @RequestParam(name = "uid") String user_id){
        QueryWrapper<UserRoleModel> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",user_id);
        this.userRoleMapper.delete(queryWrapper);
        for( UserRoleModel userRoleModel :userRoleModels){
            this.userRoleMapper.insert(userRoleModel);
        }
        return true;
    }


}
